package dcc.component;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileFilter;

import com.l2fprod.common.swing.JLinkButton;

public class SelectPhotoButton extends JLinkButton {

	private PhotoPanel photoPanel;

	private PhotoPanel getPhotoPanel() {
		return this.photoPanel;
	}

	public SelectPhotoButton(String title, PhotoPanel photoPanel) {
		super(title);
		setHorizontalTextPosition(SwingConstants.CENTER);
		setHorizontalAlignment(SwingConstants.CENTER);
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setBorderPainted(true);
		this.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
		this.photoPanel = photoPanel;
		addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File photoFile = selectPhotoFile();
				if (photoFile != null) {
					getPhotoPanel().setPhotoFile(photoFile);
					getPhotoPanel().repaint();
				}
			}
		});
	}

	private File selectPhotoFile() {
		File file = null;
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setDialogTitle("请选择照片文件：");
		FileFilter filter = new ExtensionFileFilter("JPG, JPEG and PNG",
				new String[] { "JPG", "JPEG", "PNG" });
		fileChooser.setFileFilter(filter);
		if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
		}
		return file;
	}
}
